package com.open.core.common;

import com.alibaba.fastjson2.JSON;

import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


@Data
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> {
    // system 系统消息， receive 发送， alluser 所有用户 ,heartbeat心跳
    private String name;
    //消息
    private String msg;
    //时间格式
    static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    //时间
    private String time;
    //发送者(这条消息是谁发送的)
    private String userName;

    public static TextWebSocketFrame fail(String message) {
        return new TextWebSocketFrame(JSON.toJSONString(new Result<>("system", message, LocalDateTime.now().format(formatter), null)));
    }

    public static TextWebSocketFrame success(String message) {
        return new TextWebSocketFrame(JSON.toJSONString(new Result<>("system", message, LocalDateTime.now().format(formatter), null)));
    }

    public static TextWebSocketFrame success(String name, String message) {
        return new TextWebSocketFrame(JSON.toJSONString(new Result<>(name, message, LocalDateTime.now().format(formatter),null)));
    }

    public static TextWebSocketFrame success(String name, String message, String userName) {
        return new TextWebSocketFrame(JSON.toJSONString(new Result<>(name, message, LocalDateTime.now().format(formatter),userName)));
    }

}
